package com.eric.uniqueid.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eric.uniqueid.dao.ISequenceDao;
import com.eric.uniqueid.domain.Sequence;
import com.eric.uniqueid.service.ISequenceService;



/**
 * @ClassName:     SequenceServiceImpl.java
 * @author         liuq
 * @version        V1.0  
 * @Date           2019-11-8 18:05:23
 * @Description:   serviceImpl实现类
 */
@Service("sequenceService")
public class SequenceServiceImpl implements ISequenceService {

	/**
	 * logger 日志
	 */
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	protected ISequenceDao  sequenceDao;
	/**
	 * 新增信息
	 * @param sequence
	 * @return
	 */
	@Override
	public int insert(Sequence sequence) throws Exception{
		try {
			int result = sequenceDao.insert(sequence);
			return result;
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}
	
	/**
	 * 清空数据库表
	 */
	@Override
	public void deleteAll(Sequence sequence) throws Exception {
		try {
			sequenceDao.deleteAll(sequence);
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
		
	}

	/**
	 * 获取下一个值
	 * @param sequence
	 * @return
	 */
	@Override
	public int nextVal(Sequence sequence) throws Exception {
		try {
			int i = sequenceDao.nextVal(sequence);
			return i;
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}
	
	/**
	 * 查询当前值
	 * @param sequence
	 * @return
	 */
	@Override
	public int currVal(Sequence sequence) throws Exception {
		try {
			int i = sequenceDao.currVal(sequence);
			return i;
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}
	
	/**
	 * 设置指定sequence的初始值
	 * @param sequence
	 * @return
	 */
	@Override
	public int setIncrement(Sequence sequence) throws Exception {
		try {
			int i = sequenceDao.setIncrement(sequence);
			return i;
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}
	
	/**
	 * 修改信息
	 * @param sequence
	 * @return
	 */
	@Override
	public int update(Sequence sequence) throws Exception{
		try {
			int result = sequenceDao.update(sequence);
			return result;
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}

	@Override
	public void createSequence() throws Exception {
		try {
			sequenceDao.createSequence();
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}

	@Override
	public void createCurrval() throws Exception {
		try {
			sequenceDao.createCurrval();
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}

	@Override
	public void createNextval() throws Exception {
		try {
			sequenceDao.createNextval();
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}

	@Override
	public void createSetval() throws Exception {
		try {
			sequenceDao.createSetval();
		} catch (Exception ex) {
			if (logger.isErrorEnabled()) {
				logger.error(ex.getMessage(), ex.getMessage());
			}
			ex.printStackTrace();
			throw new Exception(ex);
		}
	}

    @Override
    public Integer existTable(String tableName) throws Exception {
        try {
            int result = sequenceDao.existTable(tableName);
            return result;
        } catch (Exception ex) {
            if (logger.isErrorEnabled()) {
                logger.error(ex.getMessage(), ex.getMessage());
            }
            ex.printStackTrace();
            throw new Exception(ex);
        }
    }
}